﻿	using UnityEngine;
using System.Collections;

public class PlayerBehaviour : MonoBehaviour {
	public Renderer rendererPlayer;

	public Transform bulletSpawnPoint;
	public int powerTotal = 10;
	public int totalLife = 3;
	private int currentLife;

	private int currentPower;

	public float speed = 6.0F;
	public float jumpSpeed = 8.0F;
	public float gravity = 20.0F;
	private Vector3 moveDirection = Vector3.zero;
	private CharacterController controller;

	public AnimationClip walk;
	public AnimationClip attack;
	public AnimationClip idle;
	public AnimationClip jump;

	public Animation playerAnimation;
	public float speedWalkAnimation = 1f;
	public float speedAttackAnimation = 1f;
	private Vector3 rightDirection;
	private Vector3 leftDirection;
	public Transform mesh;

	public Transform powerPrefab;

	public int totalCoins = 0;
	public int totalCoinsToCatch = 30;

	public float positionDead = -20;

	public float powerRate = 0.5f;
	private float currentPowerRate = 0f;

	private bool directionPlayerRight = true;

	public float timeToInvencibility = 2f;
	private float currentTimeToInvencibility = 0;
	public float timeToBlink = 0.05f;
	private float currentTimeToBlink = 0f;
	private bool invencibility = false;

	//HUD
	public Renderer[] hearts;
	public TextMesh currentGoldText;
	public TextMesh goldToCatchText;
	public TextMesh currentAmountOfBulletsText;
	//HUD


	// Use this for initialization
	void Start () {
		 controller = GetComponent<CharacterController>();	
		rightDirection = mesh.localScale;
		leftDirection = mesh.localScale;
		leftDirection.x *= -1;

		currentPowerRate = powerRate;
		currentPower = powerTotal;
		currentLife = totalLife;
	}

	void RefreshHUD(){
		for (int i=0; i<hearts.Length; i++) {
			if(currentLife >= i+1){
				hearts[i].enabled = true;
			}else{
				hearts[i].enabled = false;
			}
		}

		currentGoldText.text = totalCoins.ToString();
		goldToCatchText.text = "/ "+totalCoinsToCatch;
		currentAmountOfBulletsText.text = currentPower.ToString ();

	}

	//Called once per frame
	void Update() {

		//HUD
		RefreshHUD ();
		//HUD


		moveDirection.x = Input.GetAxis("Horizontal") * speed;
		if (controller.isGrounded) {
						moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, 0);
						moveDirection = transform.TransformDirection (moveDirection);
						moveDirection *= speed;
						if (Input.GetButton ("Jump"))
								moveDirection.y = jumpSpeed;
			
				}
		else {
			playerAnimation.Play(jump.name, PlayMode.StopAll);
				}


		if (moveDirection.x == 0) {
			playerAnimation.CrossFade(idle.name);		
		}


		playerAnimation [walk.name].speed = speedWalkAnimation;

		if (moveDirection.x > 0) {
			mesh.localScale = rightDirection;	
			playerAnimation.CrossFade(walk.name);
			directionPlayerRight = true;
				} 
		else if(moveDirection.x < 0) {
			mesh.localScale = leftDirection;
			playerAnimation.CrossFade(walk.name);
			directionPlayerRight = false;
		}

		moveDirection.y -= gravity * Time.deltaTime;
		controller.Move(moveDirection * Time.deltaTime);


		if (transform.position.y <= positionDead) {
			CallGameOver();		
		}

		
		
		playerAnimation [attack.name].speed = speedAttackAnimation;

		currentPowerRate += Time.deltaTime;
		if (Input.GetButtonDown ("Fire1") ) {
			if(currentPowerRate >= powerRate && currentPower > 0){
				currentPowerRate = 0;
				GameObject projectil = Instantiate(powerPrefab.gameObject,bulletSpawnPoint.transform.position,bulletSpawnPoint.transform.rotation) as GameObject;
				projectil.GetComponent<PowerBehaviour>().right = directionPlayerRight;
				playerAnimation.Play(attack.name);
				currentPower--;
			}
		}

		if (invencibility) {
			currentTimeToBlink += Time.deltaTime;
			if(currentTimeToBlink >= timeToBlink){
				currentTimeToBlink = 0;
				rendererPlayer.enabled = !rendererPlayer.enabled;	
			}

			currentTimeToInvencibility += Time.deltaTime;
			if(currentTimeToInvencibility >= timeToInvencibility){
				rendererPlayer.enabled = true;
				currentTimeToInvencibility = 0;
				invencibility = false;
			}
		}

	}

	void ApplyDamage(){
		if(!invencibility){
			currentLife--;
			invencibility = true;
			if(currentLife <= 0){
				CallGameOver();
			}
		}
	}

	void CallGameOver(){
		Application.LoadLevel ("GameOver");
	}

	void OnControllerColliderHit(ControllerColliderHit other){
		if (other.gameObject.tag == "Enemy") {
			ApplyDamage();
		}
	}

	void OnTriggerEnter(Collider hit) {
		if (hit.transform.tag == "Gold") {
			totalCoins++;
			Destroy(hit.gameObject);
		}

		if (hit.transform.tag == "Enemy") {
			ApplyDamage();
		}

	}

}
